﻿Public Class SirenReg
    Dim registered As Boolean = False

    Private Sub JabberClientReg_OnAuthError(ByVal sender As Object, ByVal rp As System.Xml.XmlElement) Handles JabberClientReg.OnAuthError
        Try
            If Not registered Then
                LabelStatus.Text = "Registering..."
                Dim JabberClientReg As JabberClient = DirectCast(sender, JabberClient)
                JabberClientReg.Register(New jabber.JID(JabberClientReg.User, JabberClientReg.Server, Nothing))
            End If
        Catch ex As Exception
            writeLog(3, "New User Error: " & ex.Message, True)
            ButtonRegister.Enabled = True
        End Try
    End Sub

    Private Sub JabberClientReg_OnError(ByVal sender As Object, ByVal ex As System.Exception) Handles JabberClientReg.OnError
        ButtonRegister.Enabled = True
        LabelStatus.Text = "Error, try again."
    End Sub

    Private Function JabberClientReg_OnInvalidCertificate(ByVal sender As Object, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean Handles JabberClientReg.OnInvalidCertificate
        Return True
    End Function

    Private Sub JabberClientReg_OnLoginRequired(ByVal sender As Object) Handles JabberClientReg.OnLoginRequired
        ''Nothing
    End Sub

    Private Sub ButtonRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRegister.Click
        If Not TextBoxPass.Text.Equals(TextBoxPass2.Text) Then
            MessageBox.Show("Passwords do not match!")
            TextBoxPass.Text = Nothing
            TextBoxPass2.Text = Nothing
        ElseIf TextBoxUser.TextLength = Nothing Or TextBoxPass.TextLength = Nothing Then
            MessageBox.Show("You must enter a username, password, and confirm.")
        ElseIf TextBoxUser.Text.Contains("_") Or TextBoxUser.Text.Contains("@") _
        Or TextBoxUser.Text.Contains(".") Or TextBoxUser.Text.Contains("%") Then
            MessageBox.Show("Please, no special characters in your username.")
        Else
            Try
                ButtonRegister.Enabled = False
                Dim j As New jabber.JID(TextBoxUser.Text & "@" & JabberClientReg.Server.ToString)
                JabberClientReg.User = j.User
                JabberClientReg.Password = TextBoxPass.Text
                JabberClientReg.AutoLogin = False
                LabelStatus.Text = "Connecting..."
                JabberClientReg.Connect()
            Catch ex As Exception
                'MessageBox.Show("Reg Click: " & ex.Message)
            End Try
        End If
    End Sub

    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        Me.Dispose()
    End Sub

    Private Sub JabberClientReg_OnRegistered(ByVal sender As Object, ByVal iq As jabber.protocol.client.IQ) Handles JabberClientReg.OnRegistered
        Try
            LabelStatus.Text = "Please wait..."
            Dim JabberClientReg As JabberClient = DirectCast(sender, JabberClient)
            If iq.Type = IQType.result Then
                registered = True
                ''Login to the server
                JabberClientReg.Login()
                Dim newAccount As DialogResult = MessageBox.Show("Your account has been created!", "- SiNet Account Setup -", MessageBoxButtons.OK)
                If Windows.Forms.DialogResult.OK Then
                    My.Settings.sinetSirenUser = TextBoxUser.Text
                    My.Settings.sinetSirenPass = TextBoxPass.Text
                    SettingsForm.TextBoxSirenUser.Text = My.Settings.sinetSirenUser
                    SettingsForm.TextBoxSirenPass.Text = My.Settings.sinetSirenPass
                    If Main.JabberClient1.IsAuthenticated Then
                        Main.JabberClient1.Close()
                    End If
                End If
                JabberClientReg.Close()
                Me.Dispose()
            ElseIf iq.Type = IQType.error Then
                LabelStatus.Text = "---"
                ButtonRegister.Enabled = True
                MessageBox.Show("Error! Try a different username?")
            End If
        Catch ex As Exception
            'MessageBox.Show("OnRegistered: " & ex.Message)
        End Try
    End Sub

    Private Function JabberClientReg_OnRegisterInfo(ByVal sender As Object, ByVal register As jabber.protocol.iq.Register) As Boolean Handles JabberClientReg.OnRegisterInfo
        Return True
    End Function
End Class